<?php
  include("conn.php");

  $appid = 'wxb38ab1fe1dd60fed';
  $sessionKey = '45a66b765cce07b3e7026c90a419fa6c';
  $code = $_POST['code'];

  $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$sessionKey}&js_code={$code}&grant_type=authorization_code";
  $resp = file_get_contents($url);
  $robot = json_decode($resp);
  $openid = $robot->openid;
 
  $name = $_POST['name'];
  $phone = $_POST['phone'];
  $college = $_POST['college'];
  $region = $_POST['region'];
  $education = $_POST['education'];
  $register_time = $_POST['register_time'];

  $sql = "INSERT INTO `ut_user`(`openid`, `name`, `phone`, `college`, `region`, `education`, `register_time`) VALUES ('$openid','$name','$phone','$college','$region','$education', '$register_time')";
  $query = $conn->query($sql);
  $sql = "SELECT * FROM `ut_user` WHERE `openid` = '$openid'";
  $query = $conn->query($sql);
  if ($row = mysqli_fetch_assoc($query)) {
    print_r(json_encode($row));
  } else {
    print_r(json_encode(null));
  }
  $query->free_result();
  $conn->close();
?>